package com.pignest.hotel.schedule;

import org.apache.commons.lang.StringUtils;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.pignest.hotel.spring.ContextHolder;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.util.DateUtil;

public class HotelScheduler implements InitializingBean, ApplicationContextAware {
//	private static final Log LOG = LogFactory.getLog(HotelScheduler.class);
	
	private static final String TRIGGER_GROUP = "hotel";
	private static final String AUDIT_TRIGGER_NAME = "audit";
	private static final String BOOKING_TRIGGER_NAME = "booking";
	private static final String LIVING_TRIGGER_NAME = "living";

	private Scheduler scheduler;
	private String auditCronExpression;

	
	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}
	
	private String getAuditCronExpression() {
		String time = Config.getNightAuditTime();
		String[] hm = StringUtils.split(time, ":", 2);
		StringBuilder sb = new StringBuilder();
		sb.append("0 ");
		sb.append(hm[1]).append(" ");
		sb.append(hm[0]);
		sb.append(" * * ? *");
		return sb.toString();
	}
	
	/**
	 * 更新夜审时间
	 * */
	public void resetScheduleTime() {
		try {
			startAll();
		} catch (Exception e) {
			e.printStackTrace(); 
		}
	}
	
	private void startAuditJob() throws Exception {
		String expr = getAuditCronExpression();
		if (expr.equals(auditCronExpression))
			return;
//		System.out.println(expr);
		CronTrigger auditTrigger = new CronTrigger(AUDIT_TRIGGER_NAME, TRIGGER_GROUP);
		auditTrigger.setCronExpression(expr);
		if (scheduler.getTrigger(AUDIT_TRIGGER_NAME, TRIGGER_GROUP) == null) {
			JobDetail auditJob = new JobDetail("audit", NightAuditJob.class);
			scheduler.scheduleJob(auditJob, auditTrigger);
		} else {
			scheduler.rescheduleJob(AUDIT_TRIGGER_NAME, TRIGGER_GROUP, auditTrigger);
		}
		auditCronExpression = expr;
		System.out.println("next fire time: " + DateUtil.toString(auditTrigger.getNextFireTime()));
	}
	
	private void startBookingUpdateJob() throws Exception {
		String expr = "0 0/5 * * * ? *";
		CronTrigger trigger = new CronTrigger(BOOKING_TRIGGER_NAME, TRIGGER_GROUP);
		trigger.setCronExpression(expr);
		if (scheduler.getTrigger(BOOKING_TRIGGER_NAME, TRIGGER_GROUP) == null) {
			JobDetail bookingJob = new JobDetail("booking", BookingUpdateJob.class);
			scheduler.scheduleJob(bookingJob, trigger);
		} else {
			scheduler.rescheduleJob(BOOKING_TRIGGER_NAME, TRIGGER_GROUP, trigger);
		}
		System.out.println("next fire time: " + DateUtil.toString(trigger.getNextFireTime()));
	}
	
	private void startLivingUpdateJob() throws Exception {
		String expr = "0 0/3 * * * ? *";
		CronTrigger trigger = new CronTrigger(LIVING_TRIGGER_NAME, TRIGGER_GROUP);
		trigger.setCronExpression(expr);
		if (scheduler.getTrigger(LIVING_TRIGGER_NAME, TRIGGER_GROUP) == null) {
			JobDetail livingJob = new JobDetail("living", LivingUpdateJob.class);
			scheduler.scheduleJob(livingJob, trigger);
		} else {
			scheduler.rescheduleJob(LIVING_TRIGGER_NAME, TRIGGER_GROUP, trigger);
		}
		System.out.println("next fire time: " + DateUtil.toString(trigger.getNextFireTime()));
	}
	
	private void startAll() throws Exception {
		startAuditJob();
		startBookingUpdateJob();
		startLivingUpdateJob();
	}

	public void afterPropertiesSet() throws Exception {
		startAll();
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		ContextHolder.setApplicationContext(applicationContext);
	}

}
